package com.mactivity.activities;

import java.util.Map;

import com.mactivity.database.ForgotPasswordFunctions;
import com.mactivity.database.LoginFunctions;
import com.mactivity.helper.Constants;
import com.mactivity.helper.EncryptionHelper;
import com.mactivity.helper.SharedPreferenceHelper;
import com.mactivity.helper.ValidationsHelper;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * This class contains all the details for changing the password of the user.
 * This belongs to the part of a tab function in the Profile page.
 * 
 * @author Shwetha Narayan
 * 
 */
public class ChangePasswordActivity extends Activity implements OnClickListener {
	EditText editOldPassword;
	EditText editNewPassword;
	EditText editConfirmPassword;

	Button buttonChangePassword;

	Map<String, String> userDetails = null;

	/**
	 * This method is called on the activity creation. It locates the screen
	 * elements and binds the elements to the functions that occur.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.changepassword);

		editOldPassword = (EditText) findViewById(R.id.editProfileOldPwd);
		editNewPassword = (EditText) findViewById(R.id.editProfileNewPwd);
		editConfirmPassword = (EditText) findViewById(R.id.editProfileConfirmPwd);

		buttonChangePassword = (Button) findViewById(R.id.buttonProfileChangePwd);

		// On click of register check the values entered and register the user
		buttonChangePassword.setOnClickListener(this);

		// Fetch the details from Shared Preferences
		SharedPreferenceHelper sharedPref = new SharedPreferenceHelper(this);
		userDetails = sharedPref.getUserInformation();
	}

	/**
	 * This implements the on click function, which handles the Change Password.
	 * On click of the button, the password is updated after it meets the all
	 * the security constraints.
	 */
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.buttonProfileChangePwd:

			String oldpassword = editOldPassword.getText().toString();
			String newPassword = editNewPassword.getText().toString();
			String confirmPassword = editConfirmPassword.getText().toString();

			Log.d(Constants.LOG_PROFILE_TAG,
					"Onclick of Change Password: Old Password " + oldpassword
							+ " New Password :" + newPassword
							+ " COnfirm Password: " + confirmPassword);

			// Check if all the field variables are available to register
			boolean requiredFieldsPresent = ValidationsHelper
					.requiredFieldsPresent(oldpassword, newPassword,
							confirmPassword);

			// Check if the length constraint is met
			boolean passwordLengthValid = ValidationsHelper
					.checkPasswordLength(newPassword);

			if (requiredFieldsPresent && passwordLengthValid
					&& newPassword.equals(confirmPassword)) {
				// Use the Login script to check if the correct combination of
				// email Id and password is available.
				LoginFunctions loginFunctions = new LoginFunctions();
				String valid = loginFunctions.userLogin(
						userDetails.get(Constants.USER_EMAIL_ID),
						EncryptionHelper.encrypt(oldpassword));

				if (valid == null) {

					Toast.makeText(this, "Old Password is incorrect.",
							Toast.LENGTH_LONG).show();

				} else {
					ForgotPasswordFunctions forgotPasswordFunctions = new ForgotPasswordFunctions();
					forgotPasswordFunctions.resetPassword(
							userDetails.get(Constants.USER_EMAIL_ID),
							EncryptionHelper.encrypt(newPassword));

					// Password changed
					Toast.makeText(this, "Password changed successfully.",
							Toast.LENGTH_LONG).show();

					// Clean the fields
					editOldPassword.setText("");
					editNewPassword.setText("");
					editConfirmPassword.setText("");

				}

			} else {
				// If there is an error condition display it to the user.
				if (!requiredFieldsPresent) {
					Toast.makeText(this, "All the fields are required.",
							Toast.LENGTH_LONG).show();

				} else if (!passwordLengthValid) {
					Toast.makeText(
							this,
							"Length of the password should be more than 6 characters.",
							Toast.LENGTH_LONG).show();
				} else {
					Toast.makeText(
							this,
							"New Password and Confirm password does not match.",
							Toast.LENGTH_LONG).show();
				}
			}

		}

	}

	/**
	 * Override the Back button operation to navigate to the Home page.
	 */
	@Override
	public void onBackPressed() {
		Intent myIntent = new Intent(ChangePasswordActivity.this,
				HomeActivity.class);
		ChangePasswordActivity.this.startActivity(myIntent);
		finish();
	}

}
